from django.contrib.auth.backends import ModelBackend

from users.models import User


class UsernameMobileLogin(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        # 2.处理业务逻辑
        try:
            user = User.objects.get(username = username)

        except Exception as e :
            pass

            try:
                user =User.objects.get(mobile = username)
            except Exception as e:
                return None



        # 密码没有检查通过
        if not user.check_password(password):
            return None


        return user